const cloud = require('wx-server-sdk');
cloud.init({ env: cloud.DYNAMIC_CURRENT_ENV });
const db = cloud.database();

// 根据搜索关键词返回相应搜索数据
// const MAX_LIMIT = 100
exports.main = async (event, context) => {

  console.log(event);
  const _ = db.command
  let res
  if(event.orderType === 'delete') {
    db.collection('orderList').doc(event.id).update({
      data: {
        status: _.set(0)
      },
      success: function(res) {
        console.log(res.data)
      }
    })
  }else if(event.orderType === 'complete') {
    db.collection('orderList').doc(event.id).update({
      data: {
        status: _.set(2)
      },
      success: function(res) {
        console.log(res.data)
      }    
    })
  }else if(event.orderType === 'cancel') {
    db.collection('orderList').doc(event.id).update({
      data: {
        status: _.set(3)
      },
      success: function(res) {
        console.log(res.data)
      }    
    })
  }
  return res
  // const countResult = await db.collection('hotelList').count()
  // const res = await db.collection('scenicList').aggregate().match({
  //   scenicName: db.RegExp({
  //     regexp: '.*' + event.searchKeyWord,
  //     options: 'i'
  //   })
  // }).end()
  // let total = res.result.list.length
  // // const total = countResult.total
  // const batchTimes = Math.ceil(total / 100)
  // const tasks = []
  // for (let i = 0; i < batchTimes; i++) {
  //   const promise = db.collection('scenicList').skip(i * MAX_LIMIT).limit(MAX_LIMIT).get()
  //   tasks.push(promise)
  // }
  // return (await Promise.all(tasks)).reduce((acc, cur) => {
  //   return {
  //     data: acc.data.concat(cur.data),
  //     errMsg: acc.errMsg,
  //   }
  // })
}